package com.loongxun.playwithyou.ali;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.network.MyHttpRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.util.ToastView;
import com.loopj.android.http.RequestParams;

import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
public class ToAliActivity extends Activity {

    private Context context;
    //订单号，描述，价格
    private String orderNo,subject;

    private static final int SDK_PAY_FLAG = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = ToAliActivity.this;

        orderNo = getIntent().getStringExtra("orderNo");
        subject = getIntent().getStringExtra("subject");

        RequestParams params = new RequestParams();
        params.put("orderNo", orderNo);
        params.put("subject", subject);
        nHttpClient.getAll(ApiInterface.INDEX_TOALI, params, indexHandler);
    }

    //去支付宝支付
    private void toPlay(final String orderInfo){
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask((Activity) context);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };
        //吊起支付UI
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    //对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        //支付成功 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        okOrder();
                        Intent intent = new Intent();
                        intent.putExtra("ALi", true);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        failOrder();
                        Intent intent = new Intent();
                        intent.putExtra("ALi", false);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                    break;
                }
                default:
                    break;
            }
        };
    };

    private MyHttpRespenseHandler indexHandler = new MyHttpRespenseHandler() {

        @Override
        public void success(String json) {
            if (!json.equals("")) {
                toPlay(json);
            }else {
                new ToastView(context, "订单问题").show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };
}
